package com.beerandjesus.blackjack.controller;

import com.beerandjesus.blackjack.deck.Card;
import com.beerandjesus.blackjack.deck.CardCounter;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Observable;

/**
 * User: reverend
 * Date: Jul 2, 2005
 * Time: 9:28:18 PM
 */
public abstract class HandController extends Observable {

    private List hand;
    public static final Object IDENTIFIER = new Object();
    private GameController gameController;

    public HandController() {
        this.hand = new ArrayList();
    }

    public abstract boolean isDealer();

    public void checkHandValue() {
        getGameController().checkHandValues();
    }

    public void here(Card card) {
        hand.add(card);
        getGameController().displayCards(this);
        checkHandValue();
    }

    public void clear() {
        hand = new ArrayList();
    }

    public int count() {
        return CardCounter.count(hand);
    }

    public int size() {
        return hand.size();
    }

    public boolean isBlackjack() {
        return (size() == 2) && (count() == 21);
    }

    public Iterator iterator() {
        return hand.iterator();
    }

    public Card getLastCardDealt() {
        return (Card) hand.get(hand.size() - 1);
    }

    public boolean canPlay() {
        return count() < 21;
    }

    public void setGameController(GameController gameController) {
        this.gameController = gameController;
    }

    public GameController getGameController() {
        return gameController;
    }

    public boolean isBust() {
        return count() > 21;
    }

    abstract public void stay();
}
